package com.lisx.module_note.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lisx.module_note.bean.SelectIconBean;
import com.lisx.module_note.databinding.ItemSelectBinding;

import java.util.List;

public class SelectAdapter extends RecyclerView.Adapter<SelectAdapter.MyViewHolder> {

    List<SelectIconBean> iconBeans;

    public SelectAdapter(List<SelectIconBean> iconBeans) {
        this.iconBeans = iconBeans;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemSelectBinding binding = ItemSelectBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new MyViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.binding.ivRes.setImageResource(iconBeans.get(position).res);
        holder.binding.ivSel.setVisibility(iconBeans.get(position).isSelect ? View.VISIBLE : View.GONE);
        holder.binding.bgContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(iconBeans.get(position).res);
                }
                for (SelectIconBean iconBean : iconBeans) {
                    iconBean.isSelect = false;
                }
                iconBeans.get(position).isSelect = true;
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return iconBeans.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private final ItemSelectBinding binding;

        public MyViewHolder(@NonNull ItemSelectBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int res);
    }

    OnItemClickListener listener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
